package com.zhida.jiyangcenter.order

import android.app.Dialog
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.view.Gravity
import android.view.View
import android.view.Window
import android.view.WindowManager
import android.widget.ImageView
import android.widget.TextView
import androidx.annotation.NonNull
import androidx.fragment.app.DialogFragment
import androidx.lifecycle.lifecycleScope
import chooongg.base.utils.startActivity
import chooongg.base.utils.tipFail
import chooongg.http.APIException
import com.vehicle.inspection.widget.PayPwdView
import com.vehicle.inspection.widget.PwdInputMethodView
import com.zhida.jiyangcenter.R
import com.zhida.jiyangcenter.api.APIAccount
import com.zhida.jiyangcenter.api.APIPay
import com.zhida.jiyangcenter.api.BaseAPI
import com.zhida.jiyangcenter.setting.SetPayPasswordActivity
import com.zhida.jiyangcenter.utils.request
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext

class PayFragmentDialog : DialogFragment() {
    private var psw_input: PayPwdView? = null
    var listners: ((Int) -> Unit)? = null
    fun setListner(listner: ((Int) -> Unit)) {
        listners = listner
    }

    @NonNull
    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        // 使用不带Theme的构造器, 获得的dialog边框距离屏幕仍有几毫米的缝隙。
        val dialog = Dialog(requireContext(), R.style.BottomDialog)
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE) // 设置Content前设定
        dialog.setContentView(R.layout.fragment_pay_dialog)
        dialog.setCanceledOnTouchOutside(false) // 外部点击取消

        // 设置宽度为屏宽, 靠近屏幕底部。
        val window = dialog.window
        window!!.setWindowAnimations(R.style.AnimBottom)
        window.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
        val lp = window.attributes
        lp.width = WindowManager.LayoutParams.MATCH_PARENT // 宽度持平
        lp.gravity = Gravity.TOP
        window.attributes = lp

        initView(dialog)
        return dialog
    }

    private fun initView(dialog: Dialog) {
        psw_input = dialog.findViewById<View>(R.id.payPwdView) as PayPwdView
        val inputMethodView = dialog.findViewById<View>(R.id.inputMethodView) as PwdInputMethodView
        dialog.findViewById<ImageView>(R.id.iv_close).setOnClickListener {
            dismiss()
        }
        dialog.findViewById<TextView>(R.id.tv_forget).setOnClickListener {
            startActivity<SetPayPasswordActivity> {
                putExtra("type", "edit")
            }
        }
        psw_input?.setInputMethodView(inputMethodView)
        psw_input?.setInputCallBack(object : PayPwdView.InputCallBack {
            override fun onInputFinish(result: String, payPwdView: PayPwdView) {
                when (tag) {
                    "pay" -> {
                        lifecycleScope.request({ APIAccount.service().paysPasswordVerify(result) },
                            object : BaseAPI<Any>() {
                                override fun onSuccess(t: Any?, state: Int, msg: String) {
                                    lifecycleScope.request({
                                        APIPay.service().balance(
                                            arguments?.getString("code") ?: "",
                                            arguments?.getString("kind") ?: ""
                                        )
                                    }, object : BaseAPI<Any>() {
                                        override fun onSuccess(t: Any?, state: Int, msg: String) {
                                            listners?.invoke(-1)
                                        }

                                        override fun onError(e: APIException) {
                                            tipFail(e.eMessage)
                                        }

                                    })
                                }

                                override fun onError(e: APIException) {
                                    tipFail(e.eMessage)
                                }

                            })
                    }
                    "withdraw" -> {

                    }

                    else -> {

                    }
                }
            }
        })
    }

}